<%@ page language="java" import="java.util.*,com.mongodb.*,org.bson.types.ObjectId" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
         response.setContentType("text/html;charset=utf-8");  
         request.setCharacterEncoding("utf-8");           
         String username=(String)request.getParameter("username");  //文章作者
         String artId=(String)request.getParameter("id");//获取文章id
         String html=(String)request.getParameter("html");//文章内容
         String tag=(String)request.getParameter("tag");//标签
         String title=(String)request.getParameter("title");//标题
         String outline=(String)request.getParameter("outline");//片段
         String createtime=(String)request.getParameter("createtime");//发表时间
         int readNum = 0;//阅读数
         int likeNum = 0;//点赞数
         
	   	  try{   
	             // 连接到 mongodb 服务
	             MongoClient mongoClient = new MongoClient( "localhost" , 27017 );
	             DB db = mongoClient.getDB( "blog" );  //连接到数据库blog
	             DBCollection coll = db.getCollection("article");  //获取blog数据库中集合user
	            
	             DBObject article = new BasicDBObject();//定义一个Bson变量，用于存储文章信息
	             article.put("username", username);
	             article.put("html", html);
	             article.put("tag", tag);
	             article.put("outline", outline);
	             article.put("createtime", createtime);
                 article.put("title", title);
                 
                 
                 //先查找文章id是否已经存在
                 if(artId==null){
                	 article.put("readNum", readNum);
                     article.put("likeNum", likeNum);
                	 coll.insert(article);
                	 response.getWriter().write("发表成功！");
	 	       	     response.getWriter().close();
                 }else{
                	 DBObject art = new BasicDBObject();
    	             art.put("_id",new ObjectId(artId));
    	            
   	            	 BasicDBObject u= new BasicDBObject().append("$set",article);
   	            	 coll.update(art, u);
   	            	 response.getWriter().write("修改成功！");
   	 	       	     response.getWriter().close();
    	            
    
                 }
	             
	          }catch(Exception e){
	       	     response.getWriter().write("false");
	       	     response.getWriter().close();
	             System.err.println( e.getClass().getName() + ": " + e.getMessage() );
	         }    
          
     %>
</body>
</html>